<?php
header('content-type:text/html;charset=utf-8');
  if (!empty($_POST['username'])) {
      include_once './lib/fun.php';

      $username = $_POST['username'];
      $password = $_POST['password'];
      $repass = $_POST['repassword'];
      if (!$username) {
          echo "账号不能为空";
          exit;
      }
      if (!$password) {
          echo "密码不能为空";
          exit;
      }
      if (!$repass) {
          echo "确认密码不能为空";
          exit;
      }
      if ($repass !== $password) {
          echo "两次密码不一样";
          exit;
      }
      // 数据库操作
      $con = mysqlInit('127.0.0.1', 'root', 'root', 'mall');
      $sql = "select count('id') as totalcount from mall_user where `username`='{$username}'";
      $res = mysql_query($sql);
      $result = mysql_fetch_assoc($res);
      if ($result['totalcount'] > 0) {
          toMsgPage(2, '该用户已被注册');
      }
      unset($sql,$res,$result);
      // 插入到数据库
      $pass = md5Pass($password);
      if ($pass) {
          $sql = "INSERT `mall_user`(`username`,`password`,`create_time`) VALUES('{$username}','{$pass}','{$_SERVER['REQUEST_TIME']}')";
          $res = mysql_query($sql);
          if ($res) {
              // $userid = mysql_insert_id();
              toMsgPage(1, '恭喜您注册成功', 'login.php');
          // echo sprintf('恭喜您注册成功，用户名是%s,用户id是%s', $username, $userid);
          } else {
              echo mysql_error();
          }
      }
  }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>M-GALLARY|用户注册</title>
    <link type="text/css" rel="stylesheet" href="./static/css/common.css">
    <link type="text/css" rel="stylesheet" href="./static/css/add.css">
    <link rel="stylesheet" type="text/css" href="./static/css/login.css">
</head>
<body>
<div class="header">
    <div class="logo f1">
        <img src="./static/image/logo.png">
    </div>
    <div class="auth fr">
        <ul>
            <li><a href="login.php">登录</a></li>
            <li><a href="register.php">注册</a></li>
        </ul>
    </div>
</div>
<div class="content">
    <div class="center">
        <div class="center-login">
            <div class="login-banner">
                <a href="#"><img src="./static/image/login_banner.png" alt=""></a>
            </div>
            <div class="user-login">
                <div class="user-box">
                    <div class="user-title">
                        <p>用户注册</p>
                    </div>
                    <form class="login-table" name="register" id="register-form" action="register.php" method="post">
                        <div class="login-left">
                            <label class="username">用户名</label>
                            <input type="text" class="yhmiput" name="username" placeholder="Username" id="username">
                        </div>
                        <div class="login-right">
                            <label class="passwd">密码</label>
                            <input type="password" class="yhmiput" name="password" placeholder="Password" id="password">
                        </div>
                        <div class="login-right">
                            <label class="passwd">确认</label>
                            <input type="password" class="yhmiput" name="repassword" placeholder="Repassword"
                                   id="repassword">
                        </div>
                        <div class="login-btn">
                            <button type="submit">注册</button>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>
<div class="footer">
    <p><span>M-GALLARY</span> ©2017 POWERED BY IMOOC.INC</p>
</div>

</body>
<script src="./static/js/jquery-1.10.2.min.js"></script>
<script src="./static/js/layer/layer.js"></script>
<script>
    $(function () {
        $('#register-form').submit(function () {
            var username = $('#username').val(),
                password = $('#password').val(),
                repassword = $('#repassword').val();
            if (username == '' || username.length <= 0) {
                layer.tips('用户名不能为空', '#username', {time: 2000, tips: 2});
                $('#username').focus();
                return false;
            }

            if (password == '' || password.length <= 0) {
                layer.tips('密码不能为空', '#password', {time: 2000, tips: 2});
                $('#password').focus();
                return false;
            }

            if (repassword == '' || repassword.length <= 0 || (password != repassword)) {
                layer.tips('两次密码输入不一致', '#repassword', {time: 2000, tips: 2});
                $('#repassword').focus();
                return false;
            }

            return true;
        })

    })
</script>
</html>
